<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        h1 {
            margin-top: 10px;
        }
        
        #addCus {
            width: 600px;
            height: 250px;
            margin: 0 auto;
            border: 1px solid gray;
            text-align: center;
            margin-top: 30px;
            padding: 20px;
            box-shadow: 0 0 5px orange;
        }
        
        #addCus span {
            display: inline-block;
            height: 30px;
            line-height: 30px;
            width: 80px;
            text-align: left;
        }
        
        #addCus .ip {
            width: 220px;
            height: 20px;
            line-height: 20px;
            margin-top: 10px;
        }

        
        #btn {
            height: 40px;
            line-height: 20px;
            width: 100px;
            margin-top: 20px;
            margin-left: -40px;
        }
    </style>
</head>

<body>
    <h1 style="text-align: center;">修改跟踪信息</h1>
    <div id="addCus">
        <span>id：</span>
        <input id="tid" name="" type="text" value="" readonly>
        <div>
            <span class="s1">跟踪记录：</span>
            <textarea name="" id="record" class="ip" cols="30" rows="10" style="height: 100px;"></textarea>
        </div>
        <span>客户意向：</span>
        <select name="" id="intention" class="ip">
            <option value="初步沟通">初步沟通</option>
            <option value="考虑中">考虑中</option>
            <option value="有明确意向">有明确意向</option>
            <option value="签订合同">签订合同</option>
            <option value="没有意向">没有意向</option>
        </select><br>

        <input type="button" value="修改跟踪信息" id="btn">
    </div>

    <script src="../js/jquery-3.6.0.js"></script>

    <script>

        var url = location.href;
        var tid = url.substring(url.lastIndexOf("=") + 1);
        $.ajax({
            url: "/tra/"+tid,
            type: "get",
            data: {

            },
            success: function(respTxt) {
                var tk = respTxt.data;
                $("#tid").val(tid);
                //回填
                $("#record").val(tk.record);

                var intentions = $("#intention option");
                for (var opt of intentions) {
                    //alert(opt.value);
                    if (opt.value == tk.intention) {
                        opt.selected = true;
                    } else {
                        opt.selected = false;
                    }
                }
            }
        });

        //修改跟踪信息
        $("#btn").click(function() {

            var record = $("#record").val();
            var intention = $("select[id='intention']").val();

            //校验？？

            //请求
            $.ajax({
                url: "/tra",
                type: "post",
                data: {
                    tid: tid,
                    record: record,
                    intention: intention,
                    _method : "put"
                },
                success: function(respTxt) {
                    //alert(respTxt.code);
                    if (respTxt.data == 1) {
                        alert("修改成功！");
                        location.href = "track.html";
                    } else {
                        alert("修改失败！");
                    }
                }
            });
        });
    </script>
</body>

</html>
